﻿Imports System.Windows.Forms.ListViewItem

Public Class Form1
    Dim maPhieu As Integer = -1
    Private bChon As CBan = Nothing


    Private Sub QuảnLýBànToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm = New QuanLyBanvb()
        frm.ShowDialog()
    End Sub

    Private Sub QuảnLýThựcĐơnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm = New QuanLyThucDon()
        frm.ShowDialog()
    End Sub

    Private Sub ThoátToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub

    Private Sub btnTinhTien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim kn As New CKetNoi()
        '   kn.ConnectionData()
    End Sub

    Private Sub GiớiThiệuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm = New GioiThieu()
        frm.ShowDialog()
    End Sub

    'Private Sub GiúpĐỡToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GiúpĐỡToolStripMenuItem.Click

    'End Sub

    Private Sub ThoátToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Application.Exit()
    End Sub

    'Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

    'End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        LoadKhuVuc()
        load_loai()

    End Sub

    Private Sub lvwBan_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub LoadKhuVuc()
        Dim list As List(Of CKhuVuc) = KhuVucDAO.LayDanhSachKhuVuc()
        cbKhuvuc.DataSource = list
        cbKhuvuc.DisplayMember = "TenKV"
        cbKhuvuc.ValueMember = "MaKV"
    End Sub

    Private Sub cbKhuvuc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbKhuvuc.SelectedIndexChanged
        Dim kv As CKhuVuc = CType(cbKhuvuc.SelectedItem, CKhuVuc)
        load_ban(kv.MaKV)

    End Sub
    Sub load_ban(ByVal maKV As String)
        lvwBan.Clear()

        Dim list As List(Of CBan) = BanDAO.LayDanhSachByMaKV(maKV)
        For Each b As CBan In list
            Dim lvItem As New ListViewItem()


            lvItem.Text = b.TenBan
            lvItem.Tag = b
            If b.TrangThai = True Then
                lvItem.ImageIndex = 0
            Else
                lvItem.ImageIndex = 1
            End If
            lvwBan.Items.Add(lvItem)

        Next
    End Sub
    Sub load_loai()
        Dim list As List(Of CLoaiMon) = LoaiMonDao.LayDanhSach()
        cbLoaiMon.DataSource = list
        cbLoaiMon.DisplayMember = "TenLoai"
        cbLoaiMon.ValueMember = "MaLoai"
    End Sub
    Sub load_mon(ByVal maLoai As String)
        ImageList2.Images.Clear()
        lvMon.Clear()
        Dim list As List(Of CMon) = MonDAO.LayDanhSach(maLoai)
        Dim strDir As String = Environment.CurrentDirectory
        Dim i As Integer = 0
        For Each m As CMon In list
            ImageList2.Images.Add(Bitmap.FromFile(strDir + "\\" + m.ImagePath))
            Dim lv As New ListViewItem()
            lv.Text = m.TenMon + "( " + m.DonGia + "d )"
            lv.ImageIndex = i
            lv.Tag = m

            i += 1
            lvMon.Items.Add(lv)
        Next


    End Sub
    Private Sub cbLoaiMon_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbLoaiMon.SelectedIndexChanged
        Dim cl As CLoaiMon = CType(cbLoaiMon.SelectedItem, CLoaiMon)
        load_mon(cl.MaLoai)
    End Sub

    Private Sub lvMon_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvMon.SelectedIndexChanged
        '  lvwBan.se()
        If lvMon.SelectedItems.Count = 0 Then
            Return
        End If


        Dim c As CMon = CType(lvMon.SelectedItems(0).Tag, CMon)
    End Sub

    Private Sub lvwBan_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwBan.SelectedIndexChanged
        Try
            If lvwBan.SelectedItems.Count = 0 Then
                Return
            End If
            bChon = CType(lvwBan.SelectedItems(0).Tag, CBan)
            lbTenBan.Text = bChon.TenBan
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub btnGoiMon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGoiMon.Click
        If maPhieu = -1 Then

            Dim p As New CPhieu
            p.TinhTien = 0

            p.NgayTaoPhieu = Date.Now
            maPhieu = PhieuDAO.Insert(p)
            MsgBox(PhieuDAO.getId)
        End If

        Dim ischeck As Boolean = False

        If bChon Is Nothing Then
            MessageBox.Show("Ban Chua chon Ban")
        Else
            If lvMon.SelectedItems.Count = 0 Then
                MessageBox.Show("Ban Chua chon Mon")
            Else
                Dim c As CMon = CType(lvMon.SelectedItems(0).Tag, CMon)
                For Each i As ListViewItem In lvwTDofBan.Items
                    Dim m As CMon = CType(i.Tag, CMon)
                    If Not m Is Nothing Then
                        If m.MaMon = c.MaMon Then

                            Dim sl As Integer = CType(i.SubItems.Item(2).Text, Integer)
                            Dim tt As Integer = CType(i.SubItems.Item(3).Text, Integer)
                            sl += 1
                            tt += c.DonGia
                            i.SubItems.Item(2).Text = sl
                            i.SubItems.Item(3).Text = tt
                            ischeck = True


                        End If
                    End If
                Next
                If ischeck = False Then
                    Dim lv As New ListViewItem(c.TenMon)
                    lv.Tag = c
                    lv.SubItems.Add(c.DonGia)

                    lv.SubItems.Add("1")
                    lv.SubItems.Add(c.DonGia)

                    lvwTDofBan.Items.Add(lv)
                End If

            End If

        End If

        TongTien()

    End Sub

    Sub TongTien()
        Dim s As Integer = 0
        For Each i As ListViewItem In lvwTDofBan.Items
            Dim tt As Integer = CType(i.SubItems.Item(3).Text, Integer)
            s += tt

        Next
        lblTong.Text = s.ToString()
    End Sub

    Private Sub quảnLíBànToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quảnLíBànToolStripMenuItem.Click
        Dim ql As New QuanLyBanvb
        ql.Show()
    End Sub


    Private Sub QLMonAnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QLMonAnToolStripMenuItem.Click
        Dim ql As New QuanLyThucDon
        ql.Show()
    End Sub
End Class
